//
//  CellForMyExploreTableViewSectionThreeAndFour.m
//  echo
//
//  Created by dllo on 16/1/29.
//  Copyright © 2016年 Danny. All rights reserved.
//

#define WIDTH [UIScreen mainScreen].bounds.size.width / 414
#define HEIGHT [UIScreen mainScreen].bounds.size.height / 736

#import "CellForMyExploreTableViewSectionThreeAndFour.h"
#import <SDWebImage/UIButton+WebCache.h>
#import <SDWebImage/UIImageView+WebCache.h>
#import "ModelOfCellForMyExploreTableViewSectionThreeAndFour.h"
#import "ModelForSearch.h"
#import <DKNightVersion.h>
#import "SQLHandler.h"
#import "ModelOfCellForClassifyVCSectionOneToNine.h"

@implementation CellForMyExploreTableViewSectionThreeAndFour

- (void)dealloc{
    [_modelForSQL release];
    [_modelForSQLOfClassifyVC release];
    [_viewOfBackGray release];
    [_viewOfBackWhite release];
    [_button release];
    [_labelForTop release];
    [_labelForBottom release];
    [_imageViewOfFamous release];
    [_buttonForCollect release];
    [_buttonForDelete release];
    [_textFieldText release];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"alreadyDelete" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"alreadyDeleteForClassifyVC" object:nil];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.viewOfBackGray = [[UIView alloc]init];
        [self.contentView addSubview:self.viewOfBackGray];
        [self.viewOfBackGray release];

        self.viewOfBackWhite = [[UIView alloc]init];
        [self.viewOfBackGray addSubview:self.viewOfBackWhite];
        [self.viewOfBackWhite release];
        
        self.button = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.viewOfBackWhite addSubview:self.button];
    
        self.labelForTop = [[UILabel alloc]init];
        [self.viewOfBackWhite addSubview:self.labelForTop];
        self.labelForTop.dk_textColorPicker = DKColorWithColors([UIColor blackColor], [UIColor whiteColor]);
        [self.labelForTop release];
        self.labelForTop.font = [UIFont fontWithName:@"Arial" size:16.0];
        
        self.labelForBottom = [[UILabel alloc]init];
        [self.viewOfBackWhite addSubview:self.labelForBottom];
        self.labelForBottom.dk_textColorPicker = DKColorWithColors([UIColor blackColor], [UIColor whiteColor]);
        [self.labelForBottom release];
        self.labelForBottom.font = [UIFont fontWithName:@"ArialUnicodeMS" size:14.0];
        
        self.imageViewOfFamous = [[UIImageView alloc]init];
        [self.viewOfBackWhite addSubview:self.imageViewOfFamous];
        [self.imageViewOfFamous release];
        [self handleNightModel];
        
        self.buttonForCollect = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.viewOfBackWhite addSubview:self.buttonForCollect];
        
        self.buttonForDelete = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.viewOfBackWhite addSubview:self.buttonForDelete];
        [self.buttonForDelete addTarget:self action:@selector(handleDelete) forControlEvents:UIControlEventTouchUpInside];
        [self.buttonForDelete addTarget:self action:@selector(handleDeleteForClassify) forControlEvents:UIControlEventTouchUpInside];

    }
    return self;
}

- (void)handleNightModel{
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"夜间模式"]) {
        
        [DKNightVersionManager nightFalling];
        
    } else {
        
        [DKNightVersionManager dawnComing];
    }
    
    self.viewOfBackGray.dk_backgroundColorPicker = DKColorWithColors([UIColor colorWithRed:218 / 255.f green:219 / 255.f blue:216 / 255.f alpha:1], [UIColor colorWithRed:17 / 256.f green:27 / 256.f blue:47 / 256.f alpha:1]);
    self.viewOfBackWhite.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:17 / 256.f green:27 / 256.f blue:47 / 256.f alpha:1]);
}

- (void)layoutSubviews{
    
    [super layoutSubviews];
    
    self.viewOfBackGray.frame = self.contentView.frame;
    
    self.viewOfBackWhite.frame = CGRectMake(7 * WIDTH, 0, self.contentView.frame.size.width - 14 * WIDTH, self.contentView.frame.size.height);
    
    self.button.frame = CGRectMake(12 * WIDTH, 15 * HEIGHT, (self.contentView.frame.size.width - 120 * WIDTH) / 5 - 6 * WIDTH, (self.contentView.frame.size.width - 120 * WIDTH) / 5 - 6 * WIDTH);
    self.button.layer.cornerRadius = ((self.contentView.frame.size.width - 120 * WIDTH) / 5 - 6 * WIDTH) / 2;
    self.button.layer.masksToBounds = YES;
    
    self.imageViewOfFamous.frame = CGRectMake((self.contentView.frame.size.width - 120 * WIDTH) / 5 - 13 * WIDTH, (self.contentView.frame.size.width - 120 * WIDTH) / 5 - 10 * HEIGHT, (self.contentView.frame.size.width - 120 * WIDTH) / 5 / 4 + 3 * WIDTH, (self.contentView.frame.size.width - 120 * WIDTH) / 5 / 4 + 3 * WIDTH);
    self.labelForTop.frame = CGRectMake(80 * WIDTH, 15 * HEIGHT, self.contentView.frame.size.width - 160 * WIDTH, 26.4 * HEIGHT);
    self.labelForBottom.frame = CGRectMake(80 * WIDTH, 41.4 * HEIGHT, self.contentView.frame.size.width - 160 * WIDTH, 26.4 * HEIGHT);
    
    self.buttonForCollect.frame = CGRectMake(335 * WIDTH, 21 * HEIGHT, 50 * WIDTH, 30 * HEIGHT);
    [self.buttonForCollect setImage:[UIImage imageNamed:@"iconfont-shoucang1"] forState:UIControlStateNormal];

    self.buttonForDelete.frame = CGRectMake(335 * WIDTH, 21 * HEIGHT, 50 * WIDTH, 30 * HEIGHT);
    [self.buttonForDelete setImage:[UIImage imageNamed:@"iconfont-shoucang2"] forState:UIControlStateNormal];

    self.buttonForCollect.hidden = NO;
    self.buttonForDelete.hidden = YES;
    
    NSArray *arr = [SQLHandler searchData];
    for (ModelOfCellForMyExploreTableViewSectionThreeAndFour *model in arr) {
        if ([model.name isEqualToString:self.modelForSQL.name]) {
            self.buttonForCollect.hidden = YES;
            self.buttonForDelete.hidden = NO;
        }
    }
    
    NSArray *arr2 = [SQLHandler searchDataForClassifyVC];
    for (ModelOfCellForMyExploreTableViewSectionThreeAndFour *model in arr2) {
        if ([model.name isEqualToString:[self.modelForSQLOfClassifyVC objectForKey:@"name"]]) {
            self.buttonForCollect.hidden = YES;
            self.buttonForDelete.hidden = NO;
        }
    }
    
}

- (void)handleCollect{
    if ([self.delegate respondsToSelector:@selector(handleCollectAlert)]) {
        
        [self.delegate handleCollectAlert];
    }
    [[SQLHandler shareSQLTable] insertData:self.modelForSQL];
    self.buttonForCollect.hidden = YES;
    self.buttonForDelete.hidden = NO;
}

- (void)handleDelete{
    if ([self.delegate respondsToSelector:@selector(handleDeleteAlert:)]) {
        
        [self.delegate handleDeleteAlert:self.modelForSQL.name];
    }
    [[NSNotificationCenter defaultCenter] addObserverForName:@"alreadyDelete" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
        self.buttonForCollect.hidden = NO;
        self.buttonForDelete.hidden = YES;
        
    }];
}

- (void)handleDeleteForClassify{
    if ([self.delegate2 respondsToSelector:@selector(handleDeleteAlert:)]) {
        
        [self.delegate2 handleDeleteAlert:[self.modelForSQLOfClassifyVC objectForKey:@"name"]];
    }
    [[NSNotificationCenter defaultCenter] addObserverForName:@"alreadyDeleteForClassifyVC" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
        self.buttonForDelete.hidden = YES;
        self.buttonForCollect.hidden = NO;
        
    }];
}

- (void)passDataForCollect:(ModelOfCellForMyExploreTableViewSectionThreeAndFour *)model{

}

- (void)handleDeleteForCollect{

}

- (void)passDataOfCellForTableViewSectionThreeAndFour:(ModelOfCellForMyExploreTableViewSectionThreeAndFour *)model{
    self.modelForSQL = [[ModelOfCellForMyExploreTableViewSectionThreeAndFour alloc]init];
    self.modelForSQL = model;
    [self.button sd_setBackgroundImageWithURL:[NSURL URLWithString:model.avatar] forState:UIControlStateNormal];
    [self.imageViewOfFamous sd_setImageWithURL:[NSURL URLWithString:model.famous_icon]];
    self.labelForTop.text = model.name;
    self.labelForBottom.text = model.content;
    [self.buttonForCollect addTarget:self action:@selector(handleCollect) forControlEvents:UIControlEventTouchUpInside];
}

- (void)passDataOfCellForClassifyVCSectionOneToNine:(NSMutableDictionary *)dic{
    self.modelForSQLOfClassifyVC = [NSMutableDictionary dictionary];
    self.modelForSQLOfClassifyVC = dic;
    NSString *strForButton = [dic objectForKey:@"avatar"];
    NSString *strForLableOfTop = [dic objectForKey:@"name"];
    NSString *strForLableOfBottom = [dic objectForKey:@"content"];
    NSString *strForFamousImage = [dic objectForKey:@"famous_icon"];
    [self.button sd_setBackgroundImageWithURL:[NSURL URLWithString:strForButton] forState:UIControlStateNormal];
    self.labelForTop.text = strForLableOfTop;
    self.labelForBottom.text = strForLableOfBottom;
    [self.imageViewOfFamous sd_setImageWithURL:[NSURL URLWithString:strForFamousImage]];
    [self.buttonForCollect addTarget:self action:@selector(handleCollectForClassifyVC) forControlEvents:UIControlEventTouchUpInside];
}

- (void)handleCollectForClassifyVC{
    if ([self.delegate2 respondsToSelector:@selector(handleCollectAlert)]) {
        
        [self.delegate2 handleCollectAlert];
    }
    [[SQLHandler shareSQLTable] insertDataForClassifyVC:self.modelForSQLOfClassifyVC];
    self.buttonForCollect.hidden = YES;
    self.buttonForDelete.hidden = NO;
}

- (void)passDataForSearch:(ModelForSearch *)model{
    [self.button sd_setBackgroundImageWithURL:[NSURL URLWithString:model.pic] forState:UIControlStateNormal];
    NSRange range1 = [model.name rangeOfString:self.textFieldText];
    NSMutableAttributedString *attribue1 = [[NSMutableAttributedString alloc]initWithString:model.name];
    [attribue1 addAttributes:@{NSForegroundColorAttributeName: [UIColor greenColor]} range:range1];
    self.labelForTop.attributedText = attribue1;
    [attribue1 release];
    
    NSRange range2 = [model.info rangeOfString:self.textFieldText];
    NSMutableAttributedString *attribue2 = [[NSMutableAttributedString alloc]initWithString:model.info];
    [attribue2 addAttributes:@{NSForegroundColorAttributeName: [UIColor greenColor]} range:range2];
    self.labelForBottom.attributedText = attribue2;
    [attribue2 release];
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
